<template>
  <div class="account-settings-info-view" :style="{ minHeight: `${layoutContentHeight - pageHeaderHeight - 24}px` }">
    <a-form-model ref="form" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item :label="$t('authenticationCenter.securityConfiguration.longestServicelife')" prop="usefulLife">
        <a-input-number
          :disabled="!canEdit"
          style="width:100%"
          v-model="form.usefulLife"
          :min="1"
          :step="1"
          :precision="0"
        />
      </a-form-model-item>
      <a-form-model-item :label="$t('authenticationCenter.securityConfiguration.minPasswordLength')" prop="passwordMin">
        <a-input-number
          :disabled="!canEdit"
          style="width:100%"
          v-model="form.passwordMin"
          :min="6"
          :step="1"
          :max="99"
          :precision="0"
        />
      </a-form-model-item>
      <a-form-model-item :label="$t('authenticationCenter.securityConfiguration.maxPasswordLength')" prop="passwordMax">
        <a-input-number
          :disabled="!canEdit"
          style="width:100%"
          v-model="form.passwordMax"
          :min="6"
          :max="99"
          :step="1"
          :precision="0"
        />
      </a-form-model-item>
      <a-form-model-item :label="$t('authenticationCenter.securityConfiguration.passwordRule')" prop="passwordRule">
        <a-radio-group :disabled="!canEdit" v-model="form.passwordRule">
          <a-radio value="SIMPLE">
            {{ $t('authenticationCenter.securityConfiguration.numbersAndeLetters') }}
          </a-radio>
          <a-radio value="COMPLEX">
            {{ $t('authenticationCenter.securityConfiguration.numbersOrLettersOrcharacter') }}
          </a-radio>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item :label="$t('authenticationCenter.securityConfiguration.maxLoginFailures')" prop="failCount">
        <a-input-number
          :disabled="!canEdit"
          style="width:100%"
          v-model="form.failCount"
          :min="1"
          :step="1"
          :precision="0"
        />
      </a-form-model-item>
      <a-form-model-item :label="$t('common.comment')" prop="comment">
        <s-textarea :disabled="!canEdit" :limit="255" v-model="form.comment" />
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }" style="text-align:center">
        <a-button v-action:save type="primary" @click="onSubmit">
          {{ $t('button.save') }}
        </a-button>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>
<script>
import { layoutMixin } from '@/utils/mixin';
import { STextarea } from '@/components';
import { getSystemSecurity, postSystemSecurity } from '@/api/authenticationCenter/SystemSecurityController';
export default {
  name: 'SecurityConfiguration',
  mixins: [layoutMixin],
  components: {
    STextarea
  },
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      rules: {
        usefulLife: [
          {
            required: true,
            message: this.$t('common.emptyErr', {
              name: this.$t('authenticationCenter.securityConfiguration.longestServicelife')
            }),
            trigger: 'change'
          }
        ],
        passwordMin: [
          {
            required: true,
            message: this.$t('common.emptyErr', {
              name: this.$t('authenticationCenter.securityConfiguration.minPasswordLength')
            }),
            trigger: 'change'
          },
          { validator: this.validatePasswordMin }
        ],
        passwordMax: [
          {
            required: true,
            message: this.$t('common.emptyErr', {
              name: this.$t('authenticationCenter.securityConfiguration.maxPasswordLength')
            }),
            trigger: 'change'
          },
          { validator: this.validatePasswordMax }
        ],
        failCount: [
          {
            required: true,
            message: this.$t('common.emptyErr', {
              name: this.$t('authenticationCenter.securityConfiguration.maxLoginFailures')
            }),
            trigger: 'change'
          }
        ]
      },
      form: {
        passwordRule: 'SIMPLE',
        comment: ''
      },
      canEdit: this.$auth('save', this.$route.meta.permissionId)
    };
  },
  created() {
    this.getSystemSecurity();
  },
  methods: {
    // 密码长度最小值校验
    validatePasswordMin(rule, value, callback) {
      if (this.form.passwordMax != '') {
        this.$refs.form.validateField('passwordMax');
      }
      callback();
    },
    // 密码长度最大值校验
    validatePasswordMax(rule, value, callback) {
      if (value && value >= this.form.passwordMin) {
        callback();
      } else {
        callback(this.$t('authenticationCenter.securityConfiguration.passwordLengthWarn'));
      }
    },
    // 获取安全配置
    getSystemSecurity() {
      getSystemSecurity().then(res => {
        if (res.data) {
          this.form = res.data;
        }
      });
    },
    // 提交安全配置
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          postSystemSecurity(this.form).then(res => {
            if (res.code === 'sso.ok') {
              this.$message.success(res.message);
            }
          });
        }
      });
    }
  }
};
</script>
